package mailclient;

import java.awt.Component;
import javax.swing.JOptionPane;

/**
 *
 * @author Vipar
 */
public class MailClientWindow extends javax.swing.JFrame {

    /**
     * Creates new form MailClientWindow
     */
    public MailClientWindow() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        tabbedPaneParent = new javax.swing.JTabbedPane();
        menuBar = new javax.swing.JMenuBar();
        menu = new javax.swing.JMenu();
        menuNewTab = new javax.swing.JMenuItem();
        menuRemoveTab = new javax.swing.JMenuItem();
        menuExit = new javax.swing.JMenuItem();

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tabbedPaneParent.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));

        menu.setText("File");

        menuNewTab.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        menuNewTab.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/plus_16.gif"))); // NOI18N
        menuNewTab.setText("New Tab..");
        menuNewTab.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuNewTabActionPerformed(evt);
            }
        });
        menu.add(menuNewTab);

        menuRemoveTab.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        menuRemoveTab.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/minus.png"))); // NOI18N
        menuRemoveTab.setText("Remove Tab..");
        menuRemoveTab.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuRemoveTabActionPerformed(evt);
            }
        });
        menu.add(menuRemoveTab);

        menuExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0));
        menuExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/exit.gif"))); // NOI18N
        menuExit.setText("Exit..");
        menuExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuExitActionPerformed(evt);
            }
        });
        menu.add(menuExit);

        menuBar.add(menu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabbedPaneParent, javax.swing.GroupLayout.DEFAULT_SIZE, 1004, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabbedPaneParent, javax.swing.GroupLayout.DEFAULT_SIZE, 725, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuNewTabActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuNewTabActionPerformed
        // TODO add your handling code here:
        String st = JOptionPane.showInputDialog(null, "Please Enter a Tab Name");
        if(st == null) {
            return;
        }
        
        if(st.isEmpty()) {
            st = "New Tab";
        }
        tabbedPaneParent.add(new MailPanel(), st);
    }//GEN-LAST:event_menuNewTabActionPerformed

    private void menuRemoveTabActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuRemoveTabActionPerformed
        // TODO add your handling code here:
        String st = JOptionPane.showInputDialog(tabbedPaneParent, "Please Enter the name of Tab you want Removed");
        if(st == null) {
            return;
        }
        if(st.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "Nothing was removed");
            return;
        }
        
        int count = tabbedPaneParent.getComponentCount();
        if(count == 0) {
            JOptionPane.showMessageDialog(rootPane, "There are no Tabs to Remove");
            return;
        }
        for(int i = 0; i < count; i++) {
            if(tabbedPaneParent.getTitleAt(i).equals(st)) {
                tabbedPaneParent.remove(i);
                break;
            }
        }
    }//GEN-LAST:event_menuRemoveTabActionPerformed

    private void menuExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExitActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_menuExitActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MailClientWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MailClientWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MailClientWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MailClientWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MailClientWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu menu;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem menuExit;
    private javax.swing.JMenuItem menuNewTab;
    private javax.swing.JMenuItem menuRemoveTab;
    private javax.swing.JTabbedPane tabbedPaneParent;
    // End of variables declaration//GEN-END:variables
}
